﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
//using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;



namespace SublimePlayerWP.SublimePlayerWP
{
    [ToolboxItemAttribute(true)]
    public class SublimePlayerWP : System.Web.UI.WebControls.WebParts.WebPart
    {

        
        const string SUBLIMECLASSNAME = "sublime";
        const string SUBLIMECLASSLIGHTBOX = "sublime zoom";

        bool _useLightBox = false;
        string _sublimeVideoToken = string.Empty;
        string _videoPoster = string.Empty;
        string _videoWidth = string.Empty;
        string _videoHeight = string.Empty;

        string _stream1Url = string.Empty;
        bool _stream1IsHD = false;

        string _stream2Url = string.Empty;
        bool _stream2IsHD = false;

        string _stream3Url = string.Empty;
        bool _stream3IsHD = false;

        string _stream4Url = string.Empty;
        bool _stream4IsHD = false;

        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
        WebDisplayName("Player Token"),
        SPWebCategoryName("Main Settings"),
        WebPartStorage(Storage.Shared),
        WebDescription("Provide the Sublime Video Token")]
        public string SublimeVideoToken
        {
            get
            {
                return _sublimeVideoToken;
            }

            set
            {
                _sublimeVideoToken = value;
            }
        }

        [Personalizable(PersonalizationScope.Shared),
        WebBrowsable,
        WebDisplayName("Player poster"),
        WebDescription("Initial player thumbnail"),
        SPWebCategoryName("Player settings")]
        public string VideoPoster
        {
            get
            {
                return _videoPoster;
            }

            set
            {
                _videoPoster = value;
            }
        }

        //[Personalizable(PersonalizationScope.Shared),
        //WebBrowsable,
        //WebDisplayName("Use LightBox"),
        //WebDescription("Indicate if LightBox style is used"),
        //SPWebCategoryName("Player settings")]
        //public bool UseLightBox
        //{
        //    get
        //    {
        //        return _useLightBox;
        //    }
        //    set
        //    {
        //        _useLightBox = value;
        //    }
        //}

        [Personalizable(PersonalizationScope.Shared),
        WebBrowsable,
        WebDisplayName("Player width"),
        WebDescription("Player width in pixel"),
        SPWebCategoryName("Player settings")]
        public string VideoWidth
        {
            get
            {
                return _videoWidth;
            }

            set
            {
                _videoWidth = value;
            }
        }

        [Personalizable(PersonalizationScope.Shared),
        WebBrowsable,
        WebDisplayName("Player height"),
        WebDescription("Player height in pixel"),
        SPWebCategoryName("Player settings")]
        public string VideoHeight
        {
            get
            {
                return _videoHeight;
            }

            set
            {
                _videoHeight = value;
            }
        }

       [Personalizable(PersonalizationScope.Shared),
        WebBrowsable,
        WebDisplayName("Stream URL"),
        WebDescription("URL of the video"),
        SPWebCategoryName("Video 1")]
        public string Stream1URL
        {
            get
            {
                return _stream1Url;
            }

            set
            {
                _stream1Url = value;
            }
        }

        //[Personalizable(PersonalizationScope.Shared),
        //WebBrowsable,
        //WebDisplayName("HD Stream"),
        //WebDescription("The video is HD"),
        //SPWebCategoryName("Video 1")]
        //public bool Stream1IsHD
        //{
        //    get
        //    {
        //        return _stream1IsHD;
        //    }

        //    set
        //    {
        //        _stream1IsHD = value;
        //    }
        //}

        [Personalizable(PersonalizationScope.Shared),
        WebBrowsable,
        WebDisplayName("Stream URL"),
        WebDescription("URL of the video"),
        SPWebCategoryName("Video 2")]
        public string Stream2URL
        {
            get
            {
                return _stream2Url;
            }

            set
            {
                _stream2Url = value;
            }
        }

        [Personalizable(PersonalizationScope.Shared),
        WebBrowsable,
        WebDisplayName("HD Stream"),
        WebDescription("The video is HD"),
        SPWebCategoryName("Video 2")]
        public bool Stream2IsHD
        {
            get
            {
                return _stream2IsHD;
            }

            set
            {
                _stream2IsHD = value;
            }
        }

        [Personalizable(PersonalizationScope.Shared),
        WebBrowsable,
        WebDisplayName("Stream URL"),
        WebDescription("URL of the video"),
        SPWebCategoryName("Video 3")]
        public string Stream3URL
        {
            get
            {
                return _stream3Url;
            }

            set
            {
                _stream3Url = value;
            }
        }

        [Personalizable(PersonalizationScope.Shared),
        WebBrowsable,
        WebDisplayName("HD Stream"),
        WebDescription("The video is HD"),
        SPWebCategoryName("Video 3")]
        public bool Stream3IsHD
        {
            get
            {
                return _stream3IsHD;
            }

            set
            {
                _stream3IsHD = value;
            }
        }

        [Personalizable(PersonalizationScope.Shared),
        WebBrowsable,
        WebDisplayName("Stream URL"),
        WebDescription("URL of the video"),
        SPWebCategoryName("Video 4")]
        public string Stream4URL
        {
            get
            {
                return _stream4Url;
            }

            set
            {
                _stream4Url = value;
            }
        }

        [Personalizable(PersonalizationScope.Shared),
        WebBrowsable,
        WebDisplayName("HD Stream"),
        WebDescription("The video is HD"),
        SPWebCategoryName("Video 4")]
        public bool Stream4IsHD
        {
            get
            {
                return _stream4IsHD;
            }

            set
            {
                _stream4IsHD = value;
            }
        }

       

        
        protected override void Render(HtmlTextWriter writer)
        {
            // Add Sublime Player script
            
            //LightBox Settings
            if (_useLightBox)
            {
                WriteLightBoxSettings(writer);
            }

            //Add videos
            writer.WriteBeginTag("video"); //<video>
            //video settings
            WriteVideoSettings(writer);
            writer.Write(HtmlTextWriter.TagRightChar);


            WriteVideoStream(writer, _stream1Url, _stream1IsHD);

            if (_stream2Url.Trim() != string.Empty)
            {
                WriteVideoStream(writer, _stream2Url, _stream2IsHD);
            }

            if (_stream3Url.Trim() != string.Empty)
            {
                WriteVideoStream(writer, _stream3Url, _stream3IsHD);
            }

            if (_stream4Url.Trim() != string.Empty)
            {
                WriteVideoStream(writer, _stream4Url, _stream4IsHD);
            }

            writer.WriteEndTag("video"); //<video>

        }

        private void WriteVideoSettings(HtmlTextWriter writer)
        {
            if (_useLightBox)
            {
                writer.WriteAttribute("class", SUBLIMECLASSLIGHTBOX);
                writer.WriteAttribute("style", "display:none");
            }
            else
            {
                writer.WriteAttribute("class", SUBLIMECLASSNAME);
            }
            
            writer.WriteAttribute("width", _videoWidth);
            writer.WriteAttribute("height", _videoHeight);
            writer.WriteAttribute("poster", _videoPoster);
            writer.WriteAttribute("preload", "none");
            
        }

        private void WriteVideoStream(HtmlTextWriter writer, string StreamUrl, bool IsHD)
        {
            writer.WriteBeginTag("source");
            writer.WriteAttribute("src", StreamUrl);
            if (IsHD == true)
            {
                writer.WriteAttribute("data-quality", "hd");
            }
            writer.Write(HtmlTextWriter.SelfClosingTagEnd);
        }

        private void WriteLightBoxSettings(HtmlTextWriter writer)
        {
            writer.WriteBeginTag("a");
            writer.WriteAttribute("class", SUBLIMECLASSNAME);
            writer.WriteAttribute("href", _stream1Url);
            writer.Write(HtmlTextWriter.TagRightChar);
            writer.WriteBeginTag("img");
            writer.WriteAttribute("src", _videoPoster);
            writer.WriteAttribute("border", "0");
            writer.WriteAttribute("alt", "");
            writer.Write(HtmlTextWriter.SelfClosingTagEnd);
            writer.WriteEndTag("a");
        }

        protected override void CreateChildControls()
        {
            Page.Header.Controls.Add(new LiteralControl(string.Format("<script src='http://cdn.sublimevideo.net/js/{0}.js' type='text/javascript'></script>", _sublimeVideoToken)));
        }
    }
}
